package cn.edu.zafu.bigdata.zengyadian;

import java.util.Random;
import java.util.Scanner;

public class GuessNumberGame1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        while (true) {
            // 设定随机数和最大猜测次数
            int numberToGuess = random.nextInt(100) + 1; // 假设数字在1到100之间
            int maxGuesses = 5; // 最多猜测5次

            System.out.println("我已经选定了一个1到100之间的数字。你有" + maxGuesses + "次机会来猜它。");

            for (int guessCount = 0; guessCount < maxGuesses; guessCount++) {
                System.out.print("请输入你的猜测：");
                int guess = scanner.nextInt();

                if (guess < 1 || guess > 100) {
                    System.out.println("无效输入，请输入1到100之间的数字！");
                    continue;
                }

                if (guess < numberToGuess) {
                    System.out.println("太小了！");
                } else if (guess > numberToGuess) {
                    System.out.println("太大了！");
                } else {
                    System.out.println("恭喜你，猜对了！");
                    break; // 猜对了，退出循环
                }

                if (guessCount == maxGuesses - 1) {
                    System.out.println("很遗憾，你已经用完了所有的猜测机会。正确的数字是：" + numberToGuess);
                }
            }

            // 询问用户是否想继续
            System.out.print("你想再玩一轮吗？(y/n)：");
            String continueGame = scanner.next();
            if (!"y".equalsIgnoreCase(continueGame)) {
                break; // 用户不想继续，退出游戏
            }
        }

        scanner.close();
        System.out.println("感谢参与，再见！");
    }
}